package org.schedule.service.serviceImp;

import org.schedule.entity.User;
import org.schedule.mapper.UserMapper;
import org.schedule.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userInfoMapper;

    @Override
    public User findByUsername(String username) {
        return userInfoMapper.queryUserInfoByUsername(username);
    }

    //保存用户信息
    @Override
    public Boolean saveUserInfo(User userInfo) {
        // 检查用户是否存在
        User existUser = userInfoMapper.queryUserInfoByUsername(userInfo.getUsername());
        if (existUser != null) {
            return false;
        } else {
            userInfo.setRegistertime(new Date());
            userInfoMapper.insertUser(userInfo);
        }
        return true;
    }

    // 更新用户信息
    @Override
    public Boolean updateUserInfo(User userInfo) {
        User existingUser = userInfoMapper.queryUserInfoByUserId(userInfo.getId());
        if (existingUser == null) {
            return false;
        }
        return userInfoMapper.updateUser(userInfo) > 0;
    }

    //删除用户
    @Override
    public Boolean deleteUserInfo(Integer id) {
        User existingUser = userInfoMapper.queryUserInfoByUserId(id);
        if (existingUser == null) {
            return false;
        }
        return   userInfoMapper.deleteUserInfo(id) > 0;
    }
    //用户登录
    @Override
    public User login(String username, String password) {
        User existUser = userInfoMapper.queryUserInfoByUsername(username);
        //用户不存在或者密码错误则返回false
        if (existUser == null || !password.equals(existUser.getPassword())) {
            return null;
        }
        return existUser;
    }


    @Override
    public List<User> findUserInfoListByUsername(String username) {
        return userInfoMapper.querySalaryInfoListByUsername(username);
    }

    @Override
    public List<User> queryUserInfoList() {
        return userInfoMapper.queryUserInfoList();
    }

    @Override
    public User findUserInfoById(Integer id) {
        return userInfoMapper.queryUserInfoByUserId(id);
    }
}
